package com.hxzy.tms.aciton;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import com.hxzy.tms.SessionHolder;
import com.hxzy.tms.entity.Project;
import com.hxzy.tms.entity.Task;
import com.hxzy.tms.entity.Team;
import com.hxzy.tms.entity.UserTask;
import com.hxzy.tms.service.ServiceResult;
import com.hxzy.tms.service.TaskService;
import com.hxzy.tms.service.TeamService;

public class TaskAction extends AbstractAction {
    private static final long serialVersionUID = -2139256301500185323L;

    private TaskService taskService;
    private TeamService teamService;

    private Task task;
    private Date gmtStart;
    private Date gmtFinished;

    private Team team;
    private Set<Project> projects;

    private UserTask userTask;

    public String list() {
        if (StringUtils.isBlank(getParameter("project_id"))) {
            addActionError("参数错误");
            return ERROR;
        }
        long projectId = new Long(getParameter("project_id"));
        Project project = teamService.readProject(projectId).data;

        List<Task> tasks = taskService.list(projectId, 0, 1000).data;
        assign("tasks", tasks);
        assign("project", project);
        assign("team", project.getTeam());

        return SUCCESS;
    }

    public String detail() {
        if (this.task == null || this.task.getId() == null) {
            addActionError("参数错误");
            return ERROR;
        }

        Task _task = taskService.read(this.task.getId()).data;
        if (_task == null) {
            addActionError("任务不存在或者已删除");
            return ERROR;
        }
        assign("task", _task);
        assign("team", _task.getTeam());
        assign("project", _task.getProject());
        assign("projects", projects);
        Calendar calc = Calendar.getInstance();
        calc.setTimeInMillis(_task.getGmtStart() * 1000L);
        assign("gmtStart", calc.getTime());
        calc.setTimeInMillis(_task.getGmtFinished() * 1000L);
        assign("gmtFinished", calc.getTime());
        int leftHours = (int) ((_task.getGmtFinished() - (Calendar.getInstance().getTimeInMillis() / 1000)) / 60 / 60);
        assign("gmtLeft", leftHours);

        List<UserTask> userTasks = taskService.userTasks(_task.getId()).data;
        assign("userTasks", userTasks);

        int privilege = 0;
        if (SessionHolder.get() != null && _task.getUserCreate().equals(SessionHolder.get().getId())) {
            privilege = 1; // 任务的创建者
        }
        if (SessionHolder.get() != null && privilege == 0) {
            for (UserTask t : userTasks) {
                if (t.getUser().getId().equals(SessionHolder.get().getId())) {
                    privilege = 2;
                    break;
                }
            }
        }
        assign("privilege", privilege);

        return INPUT;
    }

    public String post() {
        String teamId = getParameter("team.id");
        if (teamId == null || new Long(teamId).equals(0L)) {
            addActionError("参数错误");
            return ERROR;
        }
        try {
            this.projects = teamService.projects(new Long(teamId)).data;
        } catch (Exception e) {
            logger.error("查询团队项目出错 team id: {}, exception: {}", teamId, e);
        }
        if (isGET()) {
            return INPUT;
        }

        if (isPOST()) {
            if (task == null || task.getTeam() == null || task.getTeam().getId() == null) {
                addActionError("找不到对应的团队");
                addFieldError("task.project.id", "任务必须发布到某个项目之下");
                return INPUT;
            }
            if (task == null || task.getProject() == null || task.getProject().getId() == null) {
                addFieldError("task.project.id", "任务必须发布到某个项目之下");
                return INPUT;
            }
            if (task == null || StringUtils.isBlank(task.getTitle())) {
                addFieldError("task.title", "任务名称不能为空");
                return INPUT;
            }

            if (gmtStart == null) {
                addFieldError("gmtStart", "开始时间不能为空");
                return INPUT;
            }
            if (gmtFinished == null) {
                addFieldError("gmtFinished", "结束时间不能为空");
                return INPUT;
            }

            ServiceResult<Task> result = taskService.create(task.getTeam().getId(), task.getProject().getId(),
                    task.getTitle(), task.getDesc(), task.getAttachmentUrl(), gmtStart, gmtFinished);
            switch (result.status) {
            case 1:
                addActionMessage(result.message);
                return SUCCESS;
            default:
                addFieldError("task.title", result.message);
                return INPUT;
            }
        }
        return null;
    }

    public String commit() {
        if (this.task == null || this.task.getId() == null) {
            addActionError("参数错误");
            return ERROR;
        }
        ServiceResult<UserTask> result = taskService.commit(this.task.getId(), userTask.getAttachmentUrl(),
                userTask.getRemark());
        switch (result.status) {
        case 1:
            addActionMessage("提交成功");
            return SUCCESS;
        default:
            addActionError(result.message);
            return ERROR;
        }
    }

    public String finish() {
        if (this.userTask == null || this.userTask.getId() == null) {
            addActionError("参数错误");
            return null;
        }
        ServiceResult<UserTask> result = taskService.finish(this.userTask.getId());
        writeJson(new ActionResult<UserTask>(result.status, null, result.message));
        return null;
    }

    public void setTaskService(TaskService taskService) {
        this.taskService = taskService;
    }

    public void setTeamService(TeamService teamService) {
        this.teamService = teamService;
    }

    public Task getTask() {
        return task;
    }

    public void setTask(Task task) {
        this.task = task;
    }

    public void setGmtStart(Date gmtStart) {
        this.gmtStart = gmtStart;
    }

    public void setGmtFinished(Date gmtFinished) {
        this.gmtFinished = gmtFinished;
    }

    public Date getGmtStart() {
        return gmtStart;
    }

    public Date getGmtFinished() {
        return gmtFinished;
    }

    public Set<Project> getProjects() {
        return projects;
    }

    public Team getTeam() {
        return team;
    }

    public void setTeam(Team team) {
        this.team = team;
    }

    public UserTask getUserTask() {
        return userTask;
    }

    public void setUserTask(UserTask userTask) {
        this.userTask = userTask;
    }

}
